package com.dkd.manage.service.impl;

import java.util.List;
import java.util.Objects;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.dto.ChannelVmDto;
import com.dkd.manage.domain.vo.ChannelVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.service.IChannelService;

/**
 * 售货机货道Service业务层处理
 * 
 * @author ruruihan
 * &#064;date  2025-11-04
 */
@Service
public class ChannelServiceImpl implements IChannelService
{
    @Resource
    private ChannelMapper channelMapper;

    /**
     * 查询售货机货道
     * 
     * @param id 售货机货道主键
     * @return 售货机货道
     */
    @Override
    public Channel selectChannelById(Long id)
    {
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询售货机货道列表
     * 
     * @param channel 售货机货道
     * @return 售货机货道
     */
    @Override
    public List<Channel> selectChannelList(Channel channel)
    {
        return channelMapper.selectChannelList(channel);
    }

    /**
     * 新增售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel)
    {
        channel.setCreateTime(DateUtils.getNowDate());
        return channelMapper.insertChannel(channel);
    }

/**
 * 批量插入渠道信息
 * 该方法用于一次性插入多个渠道记录，提高数据插入效率
 *
 * @param channelList 包含多个渠道信息的列表，每个元素是一个Channel对象
 * @return 返回成功插入的记录数量，如果插入失败则返回0
 */
    @Override
    public int insertChannelBatch(List<Channel> channelList) {
    // 调用channelMapper的insertChannelBatch方法执行批量插入操作
        return channelMapper.insertChannelBatch(channelList);
    }

    /**
     * 修改售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel)
    {
        channel.setUpdateTime(DateUtils.getNowDate());
        return channelMapper.updateChannel(channel);
    }

    /**
     * 批量删除售货机货道
     * 
     * @param ids 需要删除的售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids)
    {
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除售货机货道信息
     * 
     * @param id 售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id)
    {
        return channelMapper.deleteChannelById(id);
    }

    @Override
    public int countChannelBySkuIds(Long[] skuIds) {
        return channelMapper.countChannelBySkuIds(skuIds);
    }

    @Override
    public List<ChannelVo> selectChannelVoByInnerCode(String innerCode) {
        return channelMapper.selectChannelVoByInnerCode(innerCode);
    }

    @Override
    public int setChannelGoods(ChannelVmDto channelVmDto) {
        //货道关联商品
        //通过stream流用channelList中innerCode和channelCode 查询货道信息
        List<Channel> channelList = channelVmDto.getChannelList().stream().map(dto->{
            //构造查询条件
            QueryWrapper<Channel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("inner_code",dto.getInnerCode());
            queryWrapper.eq("channel_code",dto.getChannelCode());
            Channel channel = channelMapper.selectOne(queryWrapper);
            if (channel != null && !Objects.equals(channel.getSkuId(), dto.getSkuId())){
                channel.setSkuId(dto.getSkuId());
                channel.setUpdateTime(DateUtils.getNowDate());
            }
            return channel;
        }).toList();
        //批量修改
        return channelMapper.updateChannelGoodsBatch(channelList);
    }
}
